//
//  NBAClub.swift
//  Sports
//
//  Created by tiger on 2017/10/14.
//  Copyright © 2017年 Meigabay. All rights reserved.
//

import UIKit
import SQLite
import CoreLocation

public class NBAClub {
    
    public var name: String!
    public var icon: String!
    public var city: String!
    public var venue: String!
    public var introduction: String!
    
    public internal(set) var clubId: String!
    public internal(set) var createTime: TimeInterval!
    
    //媒体部
    private(set) var mediaDept: NBAMediaDept!
    
    //运营部
    private(set) var operationDept: NBAOperationDept!
    
    //事业部
    private(set) var affairsDept: NBAAffairsDept!
    
    //财务部
    private(set) var financeDept: NBAClubFinanceDept!
    
    //人事部
    private(set) var personnelDept: NBAClubPersonnelDept!
    
    //比赛管理部
    private(set) var gameDept: NBAClubGameDept!
    
    public init() {
        self.initDepts()
    }
    
    internal init(param: [String: String]) {
        self.initDepts()
    }
    
    internal init(result: Row) {
        let columns = NBAClubTableColumn()
        self.clubId = result[columns.clubId]
        self.name = result[columns.name]
        self.icon = result[columns.icon]
        self.city = result[columns.city]
        self.venue = result[columns.venue]
        self.introduction = result[columns.introduction]
        self.createTime = result[columns.createTime]
        self.initDepts()
    }
    
    private func initDepts() {
        self.initMediaDept()
        self.initOperationDept()
        self.initAffairsDept()
        self.initFinanceDept()
        self.initPersonnelDept()
        self.initGameDept()
    }
    
    /// 初始化媒体部门
    private func initMediaDept() {
        if self.mediaDept == nil {
            self.mediaDept = NBAMediaDept()
        }
    }
    
    /// 初始化运营部
    private func initOperationDept() {
        if self.operationDept == nil {
            self.operationDept = NBAOperationDept()
        }
    }
    
    /// 初始化事业部
    private func initAffairsDept() {
        if self.affairsDept == nil {
            self.affairsDept = NBAAffairsDept()
        }
    }
    
    /// 初始化财务部
    private func initFinanceDept() {
        if self.financeDept == nil {
            self.financeDept = NBAClubFinanceDept()
        }
    }
    
    /// 初始化人事部
    private func initPersonnelDept() {
        if self.personnelDept == nil {
            self.personnelDept = NBAClubPersonnelDept(club: self)
        }
    }
    
    private func initGameDept() {
        if self.gameDept == nil {
            self.gameDept = NBAClubGameDept()
        }
    }
}

public extension NBAClub {
    
    internal func startGame(game: NBAGame) {
        do {
            //人事部准备比赛，派出球员
            let workingPlayers = try self.personnelDept.preparePlayersForGame(game: game)
            self.gameDept.startGame(game: game, players: workingPlayers)
        } catch {
            
        }
    }
}


